/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp.net;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import dk.redia.lib.log.Log;
import dk.redia.lib.utils.AppApplication;
import dk.redia.lib.utils.AppConfiguration;
import dk.redia.lib.xmlrpc.XmlRpcClientResultListener;
import dk.redia.lib.xmlrpc.XmlRpcResultDumper;
import dk.redia.lib.xmlrpc.XmlRpcResultUtils;
import dk.redia.libraryapp.R;
import dk.redia.libraryapp.model.Reservation;
import dk.redia.libraryapp.xmlrpc.library.XmlRpcClient;

public class ReservationsRemoteListManager extends RemoteListManager<Reservation> {

	protected static final String logTag = "ReservationsRemoteListManager";

	@Override
	public void loadListChunk(final int offset, final int count) {
		XmlRpcClient.getInstance().getReservations(
				AppConfiguration.getInstance().getCustomerId(), 
				AppConfiguration.getInstance().getApiKey(), 
				count, 
				offset, 
				new XmlRpcClientResultListener() {
					
					public void callFailed(String message) {
						chunkLoadFailed(offset, count, message);
						Log.e(ReservationsRemoteListManager.logTag, "Failed to load reservations: " + message);
					}
					
					public void callCompleted(Object resultObj) {
						XmlRpcResultDumper.dump(resultObj);
						List<Reservation> resultReservations = new ArrayList<Reservation>();
						Map<String, Object> result = XmlRpcResultUtils.unpackStandardResult(resultObj);
						if (result==null) {
							chunkLoadFailed(offset, count, AppApplication.getAppContext().getResources().getString(R.string.failedToGetReservationsText));
						}
						Map<String, Object> data = XmlRpcResultUtils.unpackStruct(result.get("data"));
						if (data==null) {
							chunkLoadFailed(offset, count, AppApplication.getAppContext().getResources().getString(R.string.failedToGetReservationsText));
							return;
						}
						Object dataObj = data.get("data");
						if (dataObj==null) {
							chunkLoadFailed(offset, count, AppApplication.getAppContext().getResources().getString(R.string.failedToGetReservationsText));
							return;
						}
						Object[] reservationObjs = XmlRpcResultUtils.unpackArray(dataObj);
						if (reservationObjs==null) {
							chunkLoadFailed(offset, count, AppApplication.getAppContext().getResources().getString(R.string.failedToGetReservationsText));
							return;
						}
						Object totalCountObj = data.get("totalCount");
						Integer resultCount = 0;
						if (totalCountObj!=null) {
							resultCount = (Integer)totalCountObj;
						} else {
							chunkLoadFailed(offset, count, AppApplication.getAppContext().getResources().getString(R.string.failedToGetReservationsText));
							return;
						}
						
						for (int i=0;i<reservationObjs.length;i++) {
							Object reservationObj = reservationObjs[i];
							Map<String, Object> reservationMap = XmlRpcResultUtils.unpackStruct(reservationObj);
							Reservation reservation = new Reservation();
							reservation.setAuthor((String)reservationMap.get("author"));
							reservation.setHasPickupExpireDate((Boolean)reservationMap.get("hasPickupExpireDate"));
							reservation.setHasQueueNo((Boolean)reservationMap.get("hasQueueNo"));
							reservation.setHasSelfServicePickupNo((Boolean)reservationMap.get("hasSelfServicePickUpNo"));
							reservation.setPickupExpireDate(new Date(1000L*(Integer)reservationMap.get("pickupExpireDate")));
							reservation.setQueueNo((String)reservationMap.get("queueNo"));
//							reservation.setRemoteLoan((Boolean)reservationMap.get("remoteLoan"));
							reservation.setReservationId((String)reservationMap.get("reservationId"));
							reservation.setReservationOk((Boolean)reservationMap.get("reservationOk"));
							reservation.setReservationPickupBranch((String)reservationMap.get("reservationPickUpBranch"));
							reservation.setSelfServicePickupNo((String)reservationMap.get("selfServicePickUpNo"));
							reservation.setTitle((String)reservationMap.get("title"));
							// FIXME No caching
//							reservations.set(reservationsIdx, reservation);
							resultReservations.add(reservation);
						}
						chunkLoaded(offset, resultCount, resultReservations);
					}
				});
	}

}
